package com.i2works.smartluolong.manage.service.admin.impl;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.manage.dal.admin.dao.IAdminOrgDAO;
import com.i2works.smartluolong.manage.entity.admin.*;
import com.i2works.smartluolong.manage.service.admin.IAdminOrgService;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 管理员组织架构业务实现类
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/3/26 15:28
 */

@Service("adminOrgServiceImpl")
public class AdminOrgServiceImpl extends BaseCommonServiceImpl<AdminOrgQuery,AdminOrgDO> implements IAdminOrgService {

    @Autowired
    private IAdminOrgDAO adminOrgDAO;

    @Override
    public List<AdminOrgDO> queryTreeList(AdminOrgQuery query) throws ServiceException {
        try {
            return adminOrgDAO.queryTreeList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public List<AdminOrgDO> querySelect(AdminOrgQuery query) throws ServiceException {
        try {
            List<AdminOrgDO> resSelect = adminOrgDAO.queryList(query);
            List<AdminOrgDO> formatSelect = Lists.newArrayList();
            for (AdminOrgDO AdminOrgDO : resSelect) {
                if(AdminOrgDO.getOrgType() == 1){
                    formatSelect.add(AdminOrgDO);
                    AdminOrgQuery adminOrgQuery = new AdminOrgQuery();
                    adminOrgQuery.setPid(AdminOrgDO.getId());
                    adminOrgQuery.setOrgType(2);
                    List<AdminOrgDO> subResList = adminOrgDAO.queryList(adminOrgQuery);
                    if(subResList.size() > 0){
                        formatSelect.addAll(subResList);
                    }
                }
            }
            return formatSelect;
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public void save(AdminOrgDO adminOrgDO) throws ServiceException {
        try {
            if (adminOrgDO.getId() != null && adminOrgDO.getId() != 0) {
                adminOrgDAO.update(adminOrgDO);
            } else {
                adminOrgDO.setCreateName(adminOrgDO.getUpdateName());
                adminOrgDAO.save(adminOrgDO);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void updateStatus(AdminOrgDO adminOrgDO) throws ServiceException {
            try {
                if (null != adminOrgDO) {
                    adminOrgDAO.update(adminOrgDO);
                    //如果不是按钮级别类型，即可说明用于子类，然后关联设置其子类状态
                    if (!Objects.equals(adminOrgDO.getOrgType(), GlobalConstant.resType.T3)) {
                        AdminOrgQuery query = new AdminOrgQuery();
                        query.setLimit(Long.MAX_VALUE);
                        query.setPid(adminOrgDO.getId());
                        //查询其所有子类
                        List<AdminOrgDO> childrenResList = adminOrgDAO.queryList(query);
                        //循环输出子类，并设置其status为父类status
                        for (AdminOrgDO childOrg : childrenResList) {
                            childOrg.setUpdateName(adminOrgDO.getUpdateName());
                            childOrg.setStatus(adminOrgDO.getStatus());
                            //更新子类
                            adminOrgDAO.update(childOrg);
                            //更新子类的用户与组织架构状态
                            //updateUserOrgStatus(childOrg);
                        }
                    }
                    //更新用户与组织架构状态
                    //updateUserOrgStatus(adminOrgDO);
                }
            } catch (DAOException e) {
                throw new ServiceException(e);
            }
        }

    /**
     * 更新用户与组织架构状态
     */

    private void updateUserOrgStatus(AdminOrgDO adminOrgDO) throws ServiceException {
        try {
            //关联设置其角色与资源表状态
            AdminUserOrgDO userOrgDO = new AdminUserOrgDO();
            userOrgDO.setUpdateName(adminOrgDO.getUpdateName());
            userOrgDO.setOrgId(adminOrgDO.getId());
            userOrgDO.setStatus(adminOrgDO.getStatus());
            adminOrgDAO.updateUserOrgStatus(userOrgDO);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }
    
}
